﻿#region Header

/***************************************************************************
Author : Krishan Kaliraman (www.sharedwith.me)  Date: 1 Aug 2012
***************************************************************************/

#endregion Header
using System;
using System.Net;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;
using Common;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;
using SprintChart.UserControl;

namespace SprintChart
{
    public partial class CurrentSprint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["team"] != null)
            {
                string requestedTeam = Request.QueryString["team"];
                this.ViewState["TeamNameInRequest"] = requestedTeam;                
            }
            if (!IsPostBack)
            {
                FillTeams();
            }            
        }

        private void FillTeams()
        {
            string teams = ConfigurationManager.AppSettings["teams"];
            string[] allTeams=teams.Split(',');
            ddlTeams.DataSource = allTeams;
            DataBind();
            if ((!(this.ViewState["TeamNameInRequest"] == null )|| (!(this.ViewState["SelectedTeam"] == null))))
            {
                string selectedTeam=this.ViewState["TeamNameInRequest"].ToString ();
                if (!(this.ViewState["SelectedTeam"] == null))
                {
                    selectedTeam = this.ViewState["SelectedTeam"].ToString();
                }
                foreach (ListItem item in ddlTeams.Items)
                {
                    if (item.Value.ToUpper() == selectedTeam.ToUpper())
                    {
                        item.Selected = true;
                        break;
                    }
                }
                StaticItems.NodeName = selectedTeam;
                ddlTeams_SelectedIndexChanged(ddlTeams, null);
            }
        }

        private void FillCharts()
        {
            string activityTypes = ConfigurationManager.AppSettings["activityTypes"];
            DateTime dtnow = DateTime.Now;
            Sprint currentSprint = new Sprint();   
            List<ChartActivity> sprintCommittedChartActivities = currentSprint.FillAllCurrentSprintCommittedChartActivities(activityTypes);
            Table sprintChartTable = new Table();
            int count = 0;
            TableRow newRow = new TableRow();
            foreach (var sprintCommittedChartActivity in sprintCommittedChartActivities)
            {
                ActivityChartControl activityChart = (ActivityChartControl)LoadControl("~/UserControl/ActivityChartControl.ascx");
                if (sprintCommittedChartActivity.ChartActivityType == "Impediment")
                {
                    if (sprintCommittedChartActivity.ChartActivityState == "Open")
                    {
                        activityChart.SmartActivityChart.BackImage = "~/Images/ImpedimentOpen.jpg";
                    }
                    else
                    {
                        activityChart.SmartActivityChart.BackImage = "~/Images/ImpedimentClosed.jpg";
                    }
                }
                activityChart.ActivityOwner.Text += sprintCommittedChartActivity.ChartActivityOwner;
                activityChart.SmartActivityChart.Series.Add(new Series("SeriesOfActivitiesDone" + count.ToString()));
                activityChart.SmartActivityChart.Series["SeriesOfActivitiesDone" + count.ToString()].ChartType = SeriesChartType.StackedColumn;
                activityChart.SmartActivityChart.Series.Add(new Series("SeriesOfInActivitiesProgress" + count.ToString()));
                activityChart.SmartActivityChart.Series["SeriesOfInActivitiesProgress" + count.ToString()].ChartType = SeriesChartType.StackedColumn;
                activityChart.SmartActivityChart.Series.Add(new Series("SeriesOfActivitiesTodDoAndRemaining" + count.ToString()));
                activityChart.SmartActivityChart.Series["SeriesOfActivitiesTodDoAndRemaining" + count.ToString()].ChartType = SeriesChartType.StackedColumn;
                activityChart.SmartActivityChart.ChartAreas.Add(new ChartArea("ChartArea" + count.ToString()));
                ChartArea x = new ChartArea();
                activityChart.SmartActivityChart.Visible = true;
                activityChart.SmartActivityChart.Height = 250;
                activityChart.SmartActivityChart.Width = 250;
                if (sprintCommittedChartActivity.TasksWithoutEffort.Count > 0)
                {
                    FillHyperLinksForTasksWithoutEffort(activityChart, sprintCommittedChartActivity);
                }
                else
                {
                    activityChart.RemainingTasksWithoutEffort.Visible = false;
                }
                foreach (KeyValuePair<string, ChartActivityDataItemHelper> activity in sprintCommittedChartActivity.ListOfActivitiesDone)
                {
                    DataPoint newDataPoint = null;
                    newDataPoint = new DataPoint();
                    newDataPoint.ToolTip = activity.Value.ToolTipForDoneItems;
                    newDataPoint.AxisLabel = activity.Key;
                    var newvalue = new double[1];
                    newvalue[0] = activity.Value.EffortDone;
                    newDataPoint.YValues = newvalue;
                    activityChart.SmartActivityChart.Series["SeriesOfActivitiesDone" + count.ToString()].Points.Add(newDataPoint);
                    activityChart.SmartActivityChart.Series["SeriesOfActivitiesDone" + count.ToString()].Color =
                        Color.Teal;
                }
                foreach (KeyValuePair<string, ChartActivityDataItemHelper> activity in sprintCommittedChartActivity.ListOfActivitiesInProgress)
                {
                    DataPoint newDataPoint = null;
                    newDataPoint = new DataPoint();
                    newDataPoint.ToolTip = activity.Value.ToolTipForInProgressItems;
                    newDataPoint.AxisLabel = activity.Key;
                    var newvalue = new double[1];
                    newvalue[0] = activity.Value.EffortInProgress;
                    newDataPoint.YValues = newvalue;
                    activityChart.SmartActivityChart.Series["SeriesOfInActivitiesProgress" + count.ToString()].Points.Add(newDataPoint);
                    activityChart.SmartActivityChart.Series["SeriesOfInActivitiesProgress" + count.ToString()].Color =
                        Color.Gold;
                }
                foreach (KeyValuePair<string, ChartActivityDataItemHelper> activity in sprintCommittedChartActivity.ListOfActivitiesToDo)
                {

                    DataPoint newDataPoint = null;
                    newDataPoint = new DataPoint();
                    newDataPoint.ToolTip = activity.Value.ToolTipForToDoItems;
                    newDataPoint.AxisLabel = activity.Key;
                    var newvalue = new double[1];
                    newvalue[0] = activity.Value.EffortRemaining;
                    newDataPoint.YValues = newvalue;
                    activityChart.SmartActivityChart.Series["SeriesOfActivitiesTodDoAndRemaining" + count.ToString()].Points.Add(newDataPoint);
                    activityChart.SmartActivityChart.Series["SeriesOfActivitiesTodDoAndRemaining" + count.ToString()].
                        Color = Color.OrangeRed;
                }
                if (count % 4 == 0)
                {
                    newRow = new TableRow();
                }
                TableCell newCell = new TableCell();
                activityChart.ActivityLink.Text = sprintCommittedChartActivity.ChartActivityType + " " + sprintCommittedChartActivity.ChartActivityId;
                activityChart.ActivityLink.NavigateUrl = ConfigurationManager.AppSettings["TfsUrl"] + "/web/UI/Pages/WorkItems/WorkItemEdit.aspx?id=" + sprintCommittedChartActivity.ChartActivityId;
                activityChart.ActivityTitleInfo.Text = sprintCommittedChartActivity.ChartActivityTitle;
                if (ConfigurationManager.AppSettings["ShowDiscussionPageLink"] == "true")
                {
                    activityChart.DiscussionLink.Visible = true;
                    activityChart.DiscussionLink.Text = "Let's discuss";
                    activityChart.DiscussionLink.NavigateUrl = "~/DiscussPBI.aspx?activityId=" +
                                                               sprintCommittedChartActivity.ChartActivityId;
                }
                else
                {
                    activityChart.DiscussionLink.Visible = false;
                }

                newCell.Controls.Add(activityChart);
                newRow.Cells.Add(newCell);
                sprintChartTable.Rows.Add(newRow);
                count = count + 1;
            }
            onlydiv.Controls.Add(sprintChartTable);
            DateTime dtnow1 = DateTime.Now;
            TimeSpan diff = dtnow1 - dtnow;
            //Response .Write(diff.ToString());
        }

        private void FillHyperLinksForTasksWithoutEffort(ActivityChartControl activityChart, ChartActivity sprintCommittedChartActivity)
        {
            int count = 0;
            foreach (Activity linkedActivity in sprintCommittedChartActivity.TasksWithoutEffort )
            {
                count = count + 1;
                HyperLink newHyperLink = new HyperLink();
                newHyperLink.ID = linkedActivity.Id;
                newHyperLink.NavigateUrl = ConfigurationManager.AppSettings["TfsUrl"] +
                                           "/web/UI/Pages/WorkItems/WorkItemEdit.aspx?id=" + linkedActivity .Id ;
                newHyperLink.Text = linkedActivity .Id;
                newHyperLink.ToolTip = "Assigned To : " + linkedActivity.AssignedTo + " , Title : " +
                                       linkedActivity.Title;
                if (linkedActivity.ActivityType == string.Empty)
                {
                    newHyperLink.ToolTip = newHyperLink.ToolTip + " , Activity Type : Unknown";
                }
                else
                {
                    newHyperLink.ToolTip = newHyperLink.ToolTip + " , Activity Type : " + linkedActivity.ActivityType;
                }
                newHyperLink.Font.Name = "Arial";
                newHyperLink.Font.Size = 9;
                newHyperLink.Visible = true;
                activityChart.RemainingTasksWithoutEffort.Controls.Add(newHyperLink);
                if (count != sprintCommittedChartActivity.TasksWithoutEffort.Count)
                {
                    Label newLable = new Label();
                    newLable.ID = "lblSeperator" + count.ToString();
                    newLable.Text = ", ";
                    newLable.Font.Name = "Arial";
                    newLable.Font.Size = 9;
                    newLable.Visible = true;
                    activityChart.RemainingTasksWithoutEffort.Controls.Add(newLable);
                }
                
            }
        }      

        protected void ddlTeams_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlteams = (DropDownList)sender;
            string selectedTeam=ddlteams.SelectedItem.Value;
            if (selectedTeam == "Select Team")
                return;
            this.ViewState["SelectedTeam"] = selectedTeam;
            StaticItems.NodeName = selectedTeam;
            FillCharts();
        }
        
  }
}